package cn.progen.generator;




import cn.progen.constant.Config;
import cn.progen.dao.DB;
import cn.progen.model.Relation;
import cn.progen.model.Table;
import cn.progen.util.MapToObj;
import cn.progen.util.ObjUtil;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public abstract class Generator {
	public  void start(){
		DB db=new DB();
		List<Table> taList=db.getTableList(Config.Db.db_name);
		taList.forEach(item->{
			item.initImportPack();

		});

		taList.forEach(item->{
				item.getFileds().forEach(filed -> {
					String comment=filed.getColumnComment();
					Pattern pattern=Pattern.compile("\\{\\S*\\}");
					Matcher matcher=pattern.matcher(comment);
					if(matcher.find()){
						String str=matcher.group();
						String[] strs=str.substring(1,str.length()-1).split(",");
						Map<String,Object> map=new HashMap();
						if(strs[1].toLowerCase().equals("manytomany")){
							String[] strTable=strs[0].split(":");
							map.put("reTable",item.getName());
							map.put("sourceTable",strTable[0].substring(0,strTable[0].indexOf(".")));
							map.put("sourceColumn",strTable[0].substring(strTable[0].indexOf(".")+1,strTable[0].length()));
							String[] rt=strTable[1].split("#");
							map.put("reTargerColumn",rt[0]);
							map.put("reSourceColumn",rt[1]);
							map.put("targerTable",rt[2].substring(0,rt[2].indexOf(".")));
							map.put("targerColumn",rt[2].substring(rt[2].indexOf(".")+1,rt[2].length()));
							map.put("relationShip",strs[1].toLowerCase());
						}else{
							map.put("sourceTable",item.getName());
							map.put("sourceColumn",filed.getMysqlName());
							map.put("targerTable",strs[0].substring(0,strs[0].indexOf(".")));
							map.put("targerColumn",strs[0].substring(strs[0].indexOf(".")+1,strs[0].length()));
							map.put("relationShip",strs[1].toLowerCase());
						}
						Relation relation=(Relation) MapToObj.MapToObj(map,Relation.class);
						findByName(relation.getSourceTable(),taList).getRelations().add(relation);
						//item.getRelations().add(relation);
					}


				});
		});

	//	List<Table> taListCopy2= ObjUtil.deepCopy(taList);
		List<Table> taListCopy= ObjUtil.deepCopy(taList);

		taList.forEach(item->{
			item.getRelations().forEach(relation -> {
				if(relation.getRelationShip().equals("manytomany")){
					Table table=findByName(relation.getSourceTable(),taList);
					Table table1=findByName(relation.getTargerTable(),taListCopy);
					table.getRelationTables().add(table1);
				}else{
					if(relation.getRelationShip().equals("manytoone")){
						Table table=findByName(relation.getSourceTable(),taList);
						List<Table> taListCopyCur= ObjUtil.deepCopy(taList);
						Table table1=findByName(relation.getTargerTable(),taListCopyCur);
						table1.setIsList(0);
						table.getRelationTables().add(table1);
					}
				}
			});
		});



		List<Table> outList= new ArrayList<>();
		taList.forEach(item->{
			System.out.println(item.getName());
			if(item.getName().matches(".*")||item.getName().matches(".*")){
				outList.add(item);
			}
		});
		System.out.println(this.getClass()+":start out *************");
		out(outList);
		System.out.println(this.getClass()+":end out *************");
			
	};

	public  Table findByName(String name,List<Table> tables){
		Table table=null;
		for (Table item:tables
			 ) {
			if(item.getName().equals(name)){
				table=item;
			}
		}
		return  table;
	}
	public abstract void out(List<Table> taList);
}
